package com.cee.admin.common;

import com.cee.cache.redis.RedisUtils;
import com.cee.common.exception.GlobalException;
import com.cee.common.spring.SpringContextHolder;
import com.cee.common.web.result.ResultCode;
import com.cee.domain.common.form.EceBaseReportForm;
import com.cee.domain.modules.ece.entity.EceReport;
import com.cee.domain.modules.sys.entity.SysFrontUser;
import com.cee.domain.modules.sys.entity.User;
import com.cee.ds.common.constants.PermissionConstants;
import com.cee.ds.common.utils.BasePrincipalUtils;
import com.cee.ds.modules.ece.service.IEceReportService;
import com.cee.ds.modules.sys.service.ISysFrontUserService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;
import java.util.Optional;

/**
 * @author wsz
 * @since 2024/5/10
 */
public abstract class EceIndustryReportController extends BaseController {

    @Autowired
    protected IEceReportService reportService;

    @Autowired
    private RedisUtils redisUtils;

    protected <F extends EceBaseReportForm> void checkReportFormPermission(F form) {
        String permission = PermissionConstants.ECE_PREFIX + (form.getReportId() == null ? PermissionConstants.ADD : PermissionConstants.UPDATE);
        BasePrincipalUtils.checkPermission(permission);
        if (form.getReportId() != null) {
            checkReportReadPermission(form.getReportId());
        } else {
            User user = BasePrincipalUtils.currentUser();
            if (user.getCityId() != null) {
                // 检查
                Long userId = form.getUserId();
                if (userId == null) {
                    throw new GlobalException(ResultCode.PARAM_NOT_COMPLETE);
                } else {
                    SysFrontUser frontUser = SpringContextHolder.getBean(ISysFrontUserService.class).getByIdOrEx(userId);
                    if (frontUser.getCityId() != null && !Objects.equals(frontUser.getCityId(), user.getCityId())) {
                        throw new GlobalException(ResultCode.NOT_PERMISSION);
                    }
                }
            }
        }
    }

    /**
     * 检查是否有获取碳排放report的权限
     */
    protected void checkReportReadPermission(Integer reportId) {
        if (reportId == null) {
            return;
        }
        User user = BasePrincipalUtils.currentUser();
        Integer cityId = user.getCityId();
        Long uid;
        if (cityId != null) {
            // k=perm:ece:#{reportId}, v=#{uid}
            String cacheKey = "perm:ece:" + reportId;
            String cacheValue = Optional.ofNullable(redisUtils.get(cacheKey)).map(Object::toString).orElse(null);
            if (cacheValue == null) {
                EceReport report = reportService.lambdaQuery().select(EceReport::getUserId).eq(EceReport::getId, reportId).one();
                if (report == null) {
                    throw new GlobalException(ResultCode.DATA_NOT_EXIST);
                }
                uid = report.getUserId();
                redisUtils.set(cacheKey, uid.toString(), 300);
            } else {
                uid = Long.valueOf(cacheValue);
            }
            SysFrontUser frontUser = SpringContextHolder.getBean(ISysFrontUserService.class).getByIdOrEx(uid);
            Integer reportUserCityId = frontUser.getCityId();
            if (reportUserCityId != null && !Objects.equals(cityId, reportUserCityId)) {
                throw  new GlobalException(ResultCode.NOT_PERMISSION);
            }
        }
    }
}
